package v1

import (
	"RoccBlog/api"
	"RoccBlog/global"
	"RoccBlog/model"
	"RoccBlog/model/req_resp"
	v1 "RoccBlog/service/v1"
	"github.com/gin-gonic/gin"
	"strconv"
)

type BusinessTripController struct {
	api.BaseController
	Service *v1.BusinessTripService
}

// 注册出差Controller 到注册中心
func init() {
	global.RegisterController(&BusinessTripController{})
}

// RegisterController 注册BusinessTripController中的路由到gin
func (c *BusinessTripController) RegisterController(engine *gin.RouterGroup) {
	//将v1.BusinessTripService添加到Controller中，保持全局单例模式
	c.Service = global.GetService(v1.BusinessTripService{}).(*v1.BusinessTripService)

	//注册路由
	g := engine.Group("/businessTrip")
	{
		//添加出差
		g.POST("", c.Add)
		//删除出差
		g.DELETE("", c.Remove)
		//更新出差
		g.PUT("", c.Modify)
		//获取列表出差
		g.GET("/list", c.Find)
		//查找byId出差
		g.GET("", c.FindById)

		//添加出差
		g.POST("/expense", c.AddExpense)
		//删除出差
		g.DELETE("/expense", c.RemoveExpense)
		//查找byId出差
		g.GET("/expense", c.FindExpenseByID)
	}
}

// Add
// @Tags 出差管理
// @Summary 添加出差
// @Produce  json
// @Param BusinessTrip body model.BusinessTrip required "出差"
// @Success 200 {object} api.Result "成功"
// @Router /businessTrip [post]
func (c *BusinessTripController) Add(ctx *gin.Context) {
	var businessTrip model.BusinessTrip

	//绑定参数
	err := ctx.ShouldBind(&businessTrip)
	if err != nil {
		//请求参数错误处理
		c.ResponseBadRequest(ctx)
		return
	}

	//执行添加出差
	err = c.Service.Add(ctx, businessTrip)
	//处理结果
	c.HandleResponseNoResult(ctx, err, "添加出差失败", "添加出差成功")
}

// Remove
// @Tags 出差管理
// @Summary 删除出差
// @Produce  json
// @Param id query int required "出差id"
// @Success 200 {object} api.Result "成功"
// @Router /businessTrip [delete]
func (c *BusinessTripController) Remove(ctx *gin.Context) {

	//获取请求参数
	id, err := strconv.Atoi(ctx.Query("id"))
	if err != nil {
		//请求参数错误处理
		c.ResponseBadRequest(ctx)
		return
	}

	//执行参数出差
	err = c.Service.Remove(ctx, id)

	//处理结果
	c.HandleResponseNoResult(ctx, err, "删除出差失败", "删除出差成功")
}

// Modify
// @Tags 出差管理
// @Summary 修改出差
// @Produce  json
// @Param BusinessTrip body model.BusinessTrip required "出差"
// @Success 200 {object} api.Result "成功"
// @Router /businessTrip [put]
func (c *BusinessTripController) Modify(ctx *gin.Context) {
	var businessTrip model.BusinessTrip

	//请求参数绑定
	err := ctx.ShouldBind(&businessTrip)
	if err != nil {
		//处理请求参数错误
		c.ResponseBadRequest(ctx)
		return
	}

	//执行修改
	err = c.Service.Modify(ctx, businessTrip)
	c.HandleResponseNoResult(ctx, err, "修改出差失败", "修改出差成功")
}

// Find
// @Tags 出差管理
// @Summary 出差列表
// @Produce  json
// @Param pageNum query int false "页码"
// @Param pageSize query int false "每页数量"
// @Param params query req_resp.BusinessTrip false "搜索参数"
// @Success 200 {object} api.Result "成功"
// @Router /businessTrip/list [get]
func (c *BusinessTripController) Find(ctx *gin.Context) {

	//绑定查询参数
	var params req_resp.BusinessTripReq
	err := ctx.ShouldBindQuery(&params)
	if err != nil {
		//请求参数错误处理
		c.ResponseBadRequest(ctx)
		return
	}

	//查找
	pageInfo, err := c.Service.Find(ctx, params)
	//结果处理
	c.HandleResponseResult(ctx, err, "获取出差列表失败", pageInfo)
}

// FindById
// @Tags 出差管理
// @Summary 出差by id
// @Produce  json
// @Param id query int false "出差id"
// @Success 200 {object} api.Result "成功"
// @Router /businessTrip [get]
func (c *BusinessTripController) FindById(ctx *gin.Context) {

	//绑定请求参数id
	id, err := strconv.Atoi(ctx.Query("id"))
	if err != nil {
		//请求错误处理
		c.ResponseBadRequest(ctx)
		return
	}

	//查找返回查询结果
	BusinessTrip, err := c.Service.FindById(ctx, id)

	//结果处理
	c.HandleResponseResult(ctx, err, "获取出差失败", BusinessTrip)
}

// FindExpenseByID
// @Tags 出差管理
// @Summary 查询出差费用
// @Produce  json
// @Param name expenseType int required "消费类型"
// @Param name businessTripID int required "出差ID"
// @Param pageNum query int false "页码"
// @Param pageSize query int false "每页数量"
// @Success 200 {object} api.Result "成功"
// @Router /businessTrip/expense [get]
func (c *BusinessTripController) FindExpenseByID(ctx *gin.Context) {

	businessTripID, err := strconv.Atoi(ctx.Query("businessTripID"))
	if err != nil {
		c.ResponseBadRequest(ctx)
		return
	}

	//判断是否有查询条件
	expenseType, err := strconv.Atoi(ctx.Query("expenseType"))
	if err != nil {
		expenseType = -1
	}

	list, err := c.Service.FindExpenseByID(ctx, businessTripID, expenseType)

	c.HandleResponseResult(ctx, err, "查询出差费用失败", list)
}

// RemoveExpense
// @Tags 出差管理
// @Summary 删除出差费用
// @Produce  json
// @Param name id int required "消费id"
// @Param name businessTripID int required "出差ID"
// @Success 200 {object} api.Result "成功"
// @Router /businessTrip/expense [delete]
func (c *BusinessTripController) RemoveExpense(ctx *gin.Context) {
	id, err := strconv.Atoi(ctx.Query("id"))
	if err != nil {
		c.ResponseBadRequest(ctx)
		return
	}
	businessTripID, err := strconv.Atoi(ctx.Query("businessTripID"))
	if err != nil {
		c.ResponseBadRequest(ctx)
		return
	}

	err = c.Service.RemoveExpense(ctx, id, businessTripID)
	c.HandleResponseNoResult(ctx, err, "删除出差费用失败", "删除出差费用成功")
}

// AddExpense
// @Tags 出差管理
// @Summary 添加出差费用
// @Produce  json
// @Param expense body model.BusinessTripExpense false "出差费用"
// @Success 200 {object} api.Result "成功"
// @Router /businessTrip/expense [get]
func (c *BusinessTripController) AddExpense(ctx *gin.Context) {
	var expense model.BusinessTripExpense
	err := ctx.ShouldBind(&expense)
	if err != nil {
		c.ResponseBadRequest(ctx)
		return
	}

	err = c.Service.AddExpense(ctx, expense)
	c.HandleResponseNoResult(ctx, err, "添加出差费用成功", "添加出差费用失败")
}
